﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Global;
using WebHoiChoMuaBan.Models;
using System.Collections;
using WebHoiChoMuaBan.Controllers.Helpers;
using System.IO;
using WebHoiChoMuaBan.Controllers.Utility;

namespace WebHoiChoMuaBan.Controllers.Admin
{
    public class CustomerController : AdminController
    {
        public CustomerController()
        {
            //loadCommon();
        }
        //
        // GET: /Customer/

        public ActionResult Index()
        {
            if (Request.QueryString.Get("page") == null)
                return ListAllCustomers(1);
            else
                return ListAllCustomers(int.Parse(Request.QueryString.Get("page")));
        }

        public ActionResult CreateCustomer()
        {
            if (Request.QueryString.Get("cusId") == null)
            {
            }
            else
            {
                CustomerHelper hMem = new CustomerHelper();
                Customer en = hMem.getCustomer(Request.QueryString.Get("cusId") + "");
                ViewData["CusID"] = en.CusID;
                ViewData["CusAdd"] = en.CusAddress;
                ViewData["CusCellPhone"] = en.CusCellPhone;
                ViewData["CusEmail"] = en.CusEmail;
                ViewData["CusFax"] = en.CusFax;
                ViewData["CusLogoPic"] = en.CusLogo;
                ViewData["CusName"] = en.CusName;
                ViewData["CUsPassword"] = en.CusPass;
                ViewData["CusPhone"] = en.CusPhone;
            }
            return View("~/Views/Admin/CreateCustomer.aspx");
        }


        public ActionResult SaveCustomer(FormCollection form)
        {
            if (form.Get("txtCusId").Equals(""))
            {

                Customer cus = new Customer();
                cus.CusAddress = form.Get("txtCusAdd");
                cus.CusCellPhone = form.Get("txtCusCellPhone");
                cus.CusEmail = form.Get("txtCusEmail");
                cus.CusFax = form.Get("txtCusFax");
                cus.CusLogo = form.Get("txtCusLogoPic");
                cus.CusName = form.Get("txtCusName");
                cus.CusPass = Encrypt.Encrypt.GetMD5(form.Get("txtCusPassword"));
                cus.CusPhone = form.Get("txtCusPhone");
                cus.CusStatus = 1;
                foreach (string file in Request.Files)
                {
                    HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                    ImageHelper imgHelper = new ImageHelper();
                    string fileName = hpf.FileName;
                    string path = Path.Combine(Server.MapPath("~/Content/Images/Upload/Customer"), hpf.FileName);
                    cus.CusLogo = fileName;
                    imgHelper.uploadImage(path, hpf, 200, 200);

                }
                CustomerHelper cusHelper = new CustomerHelper();
                cusHelper.saveCustomer(cus);
            }
            else
            {
                CustomerHelper hMem = new CustomerHelper();
                Customer cus = hMem.getCustomer(form.Get("txtCusId"));
                cus.CusAddress = form.Get("txtCusAdd");
                cus.CusCellPhone = form.Get("txtCusCellPhone");
                cus.CusEmail = form.Get("txtCusEmail");
                cus.CusFax = form.Get("txtCusFax");
                cus.CusStatus = 1;
                foreach (string file in Request.Files)
                {
                    HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
                    ImageHelper imgHelper = new ImageHelper();
                    string fileName = hpf.FileName;
                    string path = Path.Combine(Server.MapPath("~/Content/Images/Upload/Customer"), hpf.FileName);
                    cus.CusLogo = fileName;
                    imgHelper.uploadImage(path, hpf, 200, 200);

                }
                cus.CusName = form.Get("txtCusName");
                if (!cus.CusPass.Equals(form.Get("txtCusPassword")))
                    cus.CusPass = Encrypt.Encrypt.GetMD5(form.Get("txtCusPassword"));
                cus.CusPhone = form.Get("txtCusPhone");
                hMem.saveCustomer(cus);
            }
            return ListAllCustomers(1);
        }

        [HttpPost]
        public ActionResult DeleteCustomer(string cusId)
        {
            CustomerHelper hMem = new CustomerHelper();
            hMem.DeleteCustomer(cusId);
            return ListAllCustomers(1);
        }

        [HttpPost]
        public void viewCustomer(string cusId)
        {
            CustomerHelper hMem = new CustomerHelper();
            Customer en = hMem.getCustomer(cusId);
            ViewData["CusAdd"] = en.CusAddress;
            ViewData["CusCellPhone"] = en.CusCellPhone;
            ViewData["CusEmail"] = en.CusEmail;
            ViewData["CusFax"] = en.CusFax;
            ViewData["CusLogoPic"] = en.CusLogo;
            ViewData["CusName"] = en.CusName;
            ViewData["CUsPassword"] = en.CusPass;
            ViewData["CusPhone"] = en.CusPhone;
            //return View("~/Views/Admin/CreateCustomer.aspx");
        }

        public ActionResult ListAllCustomers(int? page)
        {
            CustomerHelper hMem = new CustomerHelper();
            return ListCustomer(page, new ListCustomerDelegate(hMem.getAllCustomers));
        }

        private delegate CustomerList ListCustomerDelegate(int page, int rowPerPage);

        private ActionResult ListCustomer(int? page, ListCustomerDelegate delListMem)
        {
            int currentPage = 1;
            if (page != null)
                currentPage = (int)page;
            CustomerHelper hMem = new CustomerHelper();
            CustomerList lstNewMembers = delListMem(currentPage, Config.ITEMS_PER_PAGE);

            lstNewMembers.CurrentPage = currentPage;
            return View("~/Views/Admin/ListCustomer.aspx", lstNewMembers);
        }


        public ActionResult ChangeStatusCustomer(string cusId)
        {
            try
            {
                CustomerHelper cusHelper = new CustomerHelper();
                Customer cus = cusHelper.getCustomer(int.Parse(cusId));
                if (cus.CusStatus == 1)
                    cus.CusStatus = 0;
                else
                    cus.CusStatus = 1;
                cusHelper.Save();
            }
            catch (Exception)
            {
            }
            return Index();
        }

    }
}
